<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class StatisticsTable4 extends AdminController {

	function __construct() {
		parent::__construct();
		$this -> sidebar = "Overview-Home";
		$this -> load-> model('Table4_model');
	}

	function Index() {	
		$this ->load->model('Admin_model');
		$result['dept_list']=$this ->Admin_model->query('select distinct dept_id,dept_name from db_admin where dept_id is not null');	
		$this->load->view('admin/statisticsTable2',$result);
	}        


	function Export($dept_id=false,$start_time,$end_time,$isExport=false){		
		$role=explode(',',getSess()['sub_role_id']);
		array_push($role,getSess()['role_id']);

		if(in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role) ){
			// $dept_id=false;
		}else{
			$dept_id=getSess()['dept_id'];
		}

		$params= array('start_time'=>$start_time,'end_time'=>$end_time,'dept_id'=>$dept_id);	
		$connar['items']='dept_id,dept_name';
		$connar['where']=$this ->sqlEscape('db_table4.status=?? and db_table4.sub_time>?? and db_table4.sub_time<??',array(CommonStatus::UnDeleted[XPHEnum::Value],strtotime($start_time),strtotime($end_time)));
		$connar['join']=array('db_admin','db_admin.id=db_table4.admin_id');
		$connar['groupby']='db_admin.dept_id';
		if($dept_id!=-1){
			$connar['where'].=$this ->sqlEscape(" and db_admin.dept_id=??",array($dept_id));
		}
		$data=$this->Table4_model->get_list_full($connar);
		// foreach ($data as $k => $v) {
			$result=$data;
		// }
		foreach ($data as $k => $v) {
			$connar1['where']=$this ->sqlEscape('db_table4.status=?? and db_table4.sub_time>?? and db_table4.sub_time<?? and db_admin.dept_id=??',array(CommonStatus::UnDeleted[XPHEnum::Value],strtotime($start_time),strtotime($end_time),$v['dept_id']));
			$connar1['items']="date_format(from_unixtime(db_table4.sub_time),'%Y-%m-%d') as day";
			$connar1['join']=array('db_admin','db_admin.id=db_table4.admin_id');
			$connar1['groupby']='day';
			if($dept_id!=-1){
				$connar1['where'].=$this ->sqlEscape(" and db_admin.dept_id=??",array($v['dept_id']));
			}
			$data['ret']=$this -> Table4_model -> get_list_full($connar1);
			
			foreach ($data['ret'] as $k1 => $v1) {
				$result[$k]['data'][$k1]=array('time'=>$v1['day'],'data'=>array());
				$connar2['items']='address,teacher_dept_name,student_status,teacher_status,other_content,advice';
				$connar2['where']=$this -> sqlEscape("date_format(from_unixtime(sub_time),'%Y-%m-%d')=?? and status=??",array($v1['day'],CommonStatus::UnDeleted[XPHEnum::Value]));
				$time_data=$this -> Table4_model->get_list_full($connar2);
				// $result[$k]['data']['data']=array();
				foreach ($time_data as $k2 => $v2) {
					array_push($result[$k]['data'][$k1]['data'],array('address'=>$v2['address'],'teacher_dept_name'=>$v2['teacher_dept_name'],'student_status'=>$v2['student_status'],'teacher_status'=>$v2['teacher_status'],'other_content'=>$v2['other_content'],'advice'=>$v2['advice']));
				}
			}
		}
		$result=array('time'=>array('start_time'=>$start_time,'end_time'=>$end_time),'data'=>$result);
		$data='';
		$data.="<div class='content'><div><h3>教学状态专项检查表汇总【".$result['time']['start_time']."至".$result['time']['end_time']."】</h3></div>";
		foreach ($result['data'] as $k => $v) {
			$data.="<h5>".$v['dept_name']."</h5>";		
			foreach ($v['data'] as $k1 => $v1) {
				$data.="<div class='item'>【".$v['data'][$k1]['time']."】<br>";
				$num=0;
				foreach ($v1['data'] as $k2 => $v2) {
					$num++;
					$data.= $num.'.'.'【'.$v2['address'].'】【'.$v2['teacher_dept_name'].'】【'.$v2['student_status'].'】【'.$v2['teacher_status'].'】【'.$v2['other_content'].'】【'.$v2['advice'].'】<br>';
				}
			}
			$data.='<br>';
		}
		$data.='</div>';
		if(!$isExport){
			echo $data;
			$this->load->view('admin/statisticsTable4Export',$params);
		}else{
			$this-> load ->library('PHPWord/PHPWord');
			$PHPWord = new PHPWord();  
			$section = $PHPWord->createSection();  
			$PHPWord->addFontStyle('rStyle', array('bold'=>true,'color'=>'000000','size'=>16));  
			$PHPWord->addParagraphStyle('pStyle', array('align'=>'center'));  
			$section->addText("教学状态专项检查表汇总【".$start_time."至".$end_time."】", 'rStyle', 'pStyle');  
			$PHPWord->addFontStyle('title1', array('bold'=>true,'color'=>'000000','size'=>14));
			$section->addTextBreak(2);
			$PHPWord->addFontStyle('fStyle', array('bold'=>false,'color'=>'000000','size'=>12)); 
			$num=0; 			
			// foreach ($result['data'] as $k => $v) {	
			// 	$section->addText("【".$v['time']."】");
			// 	foreach ($v['data'] as $k1 => $v1) {
			// 		$num=$k1+1;
			// 		$section->addText($num.'.'.'【'.$v1['address'].'】【'.$v1['teacher_dept_name'].'】【'.$v1['student_status'].'】【'.$v1['teacher_status'].'】【'.$v1['other_content'].'】【'.$v1['advice'].'】');
			// 	}					
			// 	$section->addTextBreak(1);
			// }
			foreach ($result['data'] as $k => $v) {	
				$section->addText($v['dept_name'],'title1');
				$section->addTextBreak(1);
				foreach ($v['data'] as $k1 => $v1) {
					$section->addText("【".$v1['time']."】");
					foreach ($v1['data'] as $k2 => $v2) {
						$num=$k1+1;
						$section->addText($num.'.'.'【'.$v2['address'].'】【'.$v2['teacher_dept_name'].'】【'.$v2['student_status'].'】【'.$v2['teacher_status'].'】【'.$v2['other_content'].'】【'.$v2['advice'].'】');
					}					
					$section->addTextBreak(1);
				}
				$section->addTextBreak(2);				
			}
			
	        
	        $write = new PHPWord_Writer_Word2007($PHPWord); 
	       	$director = "upload/export/" . date('Ymd', time()) . "/";
	        if (!file_exists($director)) {
	            //检查是否有该文件夹，如果没有就创建，并给予最高权限
	            mkdir($director, 0777);
	        }        
			$file_name ='教学状态专项检查表汇总'.date('Ymd').'.docx';       	
	       	$write->save($director.iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址',true,$director.$file_name));
		}
		
	}
	
	

}
